package com.example.springboot.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.example.springboot.config.UserListener;
import com.example.springboot.domain.UserExcl;
import com.example.springboot.enums.ResCodeEnum;
import com.example.springboot.services.UserService;
import com.example.springboot.utils.ThreadLocalUtil;
import com.example.springboot.vo.ResVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.UUID;

@RestController
@RequestMapping(value = "/file")
@Slf4j
public class FileController {

    @Value("${upload}")
    String upload;

    @Autowired
    UserService userService;

    @ResponseBody
    @PostMapping("/upload")
    public String fileUpload( @RequestParam("files") MultipartFile files) throws IOException {

        // win系统 上传路径保存设置

        // 获取项目路径
        File projectPath = new File(ResourceUtils.getURL("classpath:").getPath());
        // 绝对路径=项目路径+自定义路径
        File pathFile = new File(upload+"img");
        if (!pathFile.exists()) {
            pathFile.mkdirs();
        }
        //上传文件地址
        UUID uuid = UUID.randomUUID();
        File serverFile = new File(pathFile, uuid + "_" + files.getOriginalFilename());
        files.transferTo(serverFile);

        String imgPath = ( uuid + "_" + files.getOriginalFilename()).replace("\\", "/");


        return imgPath;
    }

    @ResponseBody
    @PostMapping("/exclUpload")
    public ResVO<String> exclUpload(@RequestParam("files") MultipartFile files) throws IOException {
        // 获取项目路径
        File projectPath = new File(ResourceUtils.getURL("classpath:").getPath());

        // 绝对路径=项目路径+自定义路径
        File pathFile = new File(upload+"excl");
        if (!pathFile.exists()) {
            pathFile.mkdirs();
        }
        //上传文件地址
        UUID uuid = UUID.randomUUID();
        File serverFile = new File(pathFile, uuid + "_" + files.getOriginalFilename());
        files.transferTo(serverFile);

        String imgPath = ( uuid + "_" + files.getOriginalFilename()).replace("\\", "/");

        ExcelReaderBuilder readerBuilder = EasyExcel.read(upload+"excl\\"+imgPath, UserExcl.class,new UserListener(userService));

        ExcelReaderSheetBuilder sheetBuilder = readerBuilder.sheet();

        sheetBuilder.doRead();

        Object count = ThreadLocalUtil.getObject();

        log.info("--------已更新"+ count + "条数据");

        if(count == null || count.equals(0) ){
            return new ResVO<>(ResCodeEnum.ADDER.getResCode(),"导入失败，文件数据已存在","");
        }

        return new ResVO<>(ResCodeEnum.ADDOK.getResCode(),"成功导入" + ThreadLocalUtil.getObject() +"条数据",imgPath);
    }

}
